package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @date ：Created in 2019/7/5
 * @description ：
 * @version: 1.0
 */
@Service
public class PermissionImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 新增权限
     *
     * @param permission 参数是权限对象
     */
    @Override
    @Transactional
    public void add(Permission permission) {

        permissionDao.add(permission);

    }

    /**
     * @param queryPageBean 分页对象
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        //使用pageHelper 开启分页查询
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        // 获取分页数据
        Page<Permission> page = permissionDao.selectByCondition(queryPageBean.getQueryString());

        // 封装分页数据，返回分页对象
        return new PageResult(page.getTotal(), page.getResult());

    }

    /**
     * 基于id删除权限
     *
     * @param id
     */
    @Override
    @Transactional
    public void deleteById(Integer id) {

        Long count = permissionDao.countRoleByPermissionId(id);

        if (count > 0) {

            throw new RuntimeException("当前权限使用中,不能删除");
        }

        permissionDao.deletePermissionById(id);


    }

    /**
     * 基于id查找权限数据
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {

        return permissionDao.findById(id);
    }

    /**
     * 修改权限数据
     * @param permission
     */
    @Override
    @Transactional
    public void edit(Permission permission) {

        permissionDao.edit(permission);

    }

    /**
     * 查询所有权限
     * @return
     */
    @Override
    public List<Permission> findAll() {

        return permissionDao.findAll();
    }

}
